package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.*;
import com.sg.entity.RoleMember;
import com.sg.service.base.framework.MRoleMemberService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.RoleMemberMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MRoleMemberServiceImpl
        extends ServiceImpl<RoleMemberMapper, RoleMember>
        implements MRoleMemberService {

    @Autowired
    RoleMemberMapper roleMemberMapper;

    /**
     * 1-2-13查询角色人员详情[335]
     * gen by moon at 4/23/2023, 11:15:23 AM
     */
    @Trace(operationName = "1-2-13查询角色人员详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RoleMember queryRoleRelDetail(QueryRoleRelDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<RoleMember> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<RoleMember> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                RoleMember retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-13查询角色人员列表[334]
     * gen by moon at 10/16/2022, 11:09:06 AM
     */
    @Trace(operationName = "1-2-13查询角色人员列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RoleMember> queryRoleRelList(QueryRoleRelListReq reqDto) {

        try {
            QueryWrapper<RoleMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(RoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<RoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-13新增角色人员[331]
     * gen by moon at 10/16/2022, 11:09:04 AM
     */
    @Trace(operationName = "1-2-13新增角色人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addRoleRel(RoleMember roleMember) {
        roleMember.setRoleMemberId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        roleMember.setRelationshipValid("FALSE");
        roleMember.setIsArchive("FALSE");
        roleMember.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        roleMember.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (roleMember != null && roleMember.getSubjectLifeCycle() != null && roleMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            roleMember.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        roleMember.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        roleMember.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (roleMember != null && roleMember.getSubjectLifeCycle() != null && roleMember.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            roleMember.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        roleMember.setIsValid("FALSE");
        if (super.save(roleMember)) {
            return roleMember.getRoleMemberId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-2-13批量查询角色人员[1354]
     * gen by moon at 10/16/2022, 11:09:07 AM
     */
    @Trace(operationName = "1-2-13批量查询角色人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RoleMember> batchQueryRoleRel(BatchQueryRoleRelReq reqDto) {

        try {
            QueryWrapper<RoleMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getRoleRelList() == null || reqDto.getRoleRelList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("role_member_id", reqDto.getRoleRelList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(RoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<RoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-13批量修改角色人员[1560]
     * gen by moon at 10/16/2022, 11:09:08 AM
     */
    @Trace(operationName = "1-2-13批量修改角色人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateRoleRel(List<RoleMember> roleMember) {

        if (roleMember == null || roleMember.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(roleMember);

    }

    /**
     * 1-2-13批量角色Code查角色人员[1742]
     * gen by moon at 10/16/2022, 11:09:11 AM
     */
    @Trace(operationName = "1-2-13批量角色Code查角色人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RoleMember> batchQueryRoleRelByCode(BatchQueryRoleRelByCodeReq reqDto) {

        try {
            QueryWrapper<RoleMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getRoleRelList() == null || reqDto.getRoleRelList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("role_code", reqDto.getRoleRelList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(RoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<RoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-13批量角色ID查角色人员列表[1726]
     * gen by moon at 10/16/2022, 11:09:10 AM
     */
    @Trace(operationName = "1-2-13批量角色ID查角色人员列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RoleMember> batchQueryRoleRelByRoleIds(BatchQueryRoleRelByRoleIdsReq reqDto) {

        try {
            QueryWrapper<RoleMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSubjectLifeCycle("CURRENTLY_VALID");
            if (reqDto.getRoleRelList() == null || reqDto.getRoleRelList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("role_id", reqDto.getRoleRelList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(RoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<RoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-13批量删除角色人员[1559]
     * gen by moon at 10/16/2022, 11:09:08 AM
     */
    @Trace(operationName = "1-2-13批量删除角色人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteRoleRel(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-2-13修改角色人员[333]
     * gen by moon at 10/16/2022, 11:09:05 AM
     */
    @Trace(operationName = "1-2-13修改角色人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRoleRel(RoleMember roleMember) {

        return super.updateById(roleMember);
    }

    /**
     * 1-2-13删除角色人员[332]
     * gen by moon at 10/16/2022, 11:09:04 AM
     */
    @Trace(operationName = "1-2-13删除角色人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRoleRel(String roleMemberId) {

        return super.removeById(roleMemberId);
    }

    /**
     * 1-2-09批量查询角色人员列表By账号ID[1709]
     * gen by moon at 10/16/2022, 11:09:09 AM
     */
    @Trace(operationName = "1-2-09批量查询角色人员列表By账号ID")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RoleMember> batchQueryRoleRelListByIds(BatchQueryRoleRelListByIdsReq reqDto) {

        try {
            QueryWrapper<RoleMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getRoleRelList() == null || reqDto.getRoleRelList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("user_id", reqDto.getRoleRelList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(RoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<RoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-08查询用于权限角色列表[2184]
     * gen by moon at 5/21/2024, 2:01:43 AM
     */
    @Trace(operationName = "1-2-08查询用于权限角色列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RoleMember> queryRoleOfPowerList(QueryRoleOfPowerListReq reqDto) {

        try {
            QueryWrapper<RoleMember> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(RoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<RoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删角色人员测试[4865]
     * gen by moon at 10/16/2022, 11:09:13 AM
     */
    @Trace(operationName = "M1批量删角色人员测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteRoleMemberTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * M1批量查询角色人员测试[4870]
     * gen by moon at 10/16/2022, 11:09:13 AM
     */
    @Trace(operationName = "M1批量查询角色人员测试")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<RoleMember> batchQueryRoleMemberTestData(BatchQueryRoleMemberTestDataReq reqDto) {

        try {
            QueryWrapper<RoleMember> queryWrapper = new QueryWrapper<>();

            if (reqDto.getRoleMemberTestDataList() == null || reqDto.getRoleMemberTestDataList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("user_id", reqDto.getRoleMemberTestDataList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(RoleMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(RoleMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<RoleMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-13查询角色人员详情[8946]
     * gen by moon at 4/2/2024, 11:30:30 PM
     */
    @Trace(operationName = "1-2-13查询角色人员详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RoleMember queryRoleMemberDetail(QueryRoleMemberDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<RoleMember> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<RoleMember> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                RoleMember retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
